//
//  HomeRecomedModel.m
//  OnionHome
//
//  Created by fengjie on 2019/9/27.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeRecomedModel.h"

@implementation HomeRecomedModel
-(void)setContenx:(NSString *)contenx{
   
    contenx =  [contenx stringByReplacingOccurrencesOfString:@"max-width" withString:@"width"];
    contenx =  [contenx stringByReplacingOccurrencesOfString:@"98%" withString:[NSString stringWithFormat:@"%fpx",(KScreenWidth-32)]];
//    contenx =  [contenx stringByReplacingOccurrencesOfString:@"auto" withString:@"100px"];

     _contenx = contenx;
    
//    NSAttributedString * attrstr = [NSAttributedString htmlString:contenx];
    self.attrstr = [self getAttributedStr:contenx];
    
    DLog(@"%@",contenx);
    
    
}

-(NSAttributedString *)getAttributedStr:(NSString *)HTMLString{
    
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)
                              };
    NSData *data = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];   // 调整行间距
//    paragraphStyle.lineSpacing = 8.0;
    paragraphStyle.alignment = NSTextAlignmentJustified;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
    
//    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attributedString.length)];
    
    return attributedString;

}

- (NSString *)filterImage:(NSString *)html{
    DLog(@"%@",html);
    NSMutableArray *resultArray = [NSMutableArray array];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(img|IMG)(.*?)(/>|></img>|>)" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
    NSArray *result = [regex matchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length)];
    for (NSTextCheckingResult *item in result) {
        NSString *imgHtml = [html substringWithRange:[item rangeAtIndex:0]];
        NSArray *tmpArray = nil;
        if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) {
            tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];
        } else if ([imgHtml rangeOfString:@"src="].location != NSNotFound) {
            tmpArray = [imgHtml componentsSeparatedByString:@"src="];
        }
        if (tmpArray.count >= 2) {
            NSString *src = tmpArray[1];
            NSUInteger loc = [src rangeOfString:@"\""].location;
            if (loc != NSNotFound) {
                src = [src substringToIndex:loc];
                [resultArray addObject:src];
                NSString * orldstr_key = [NSString stringWithFormat:@"src=\"%@\"",src];
                NSString *aPath3=[NSString stringWithFormat:@"src=\"%@/Documents/%@.png\"",NSHomeDirectory(),src];
                
                html =  [html stringByReplacingOccurrencesOfString:orldstr_key withString:aPath3];
            }
        }
    }
    DLog(@"%@",html);
    return html;
}
@end
